import { createStore, Store, useStore as useStoreVuex } from 'vuex'
import { IRootState, IRootWithModule, IStoreType } from './types'
import { loginModule as login } from './login'
import { systemModule as system } from './system'

const store = createStore<IRootState>({
  state: () => {
    return {
      name: 'chanchaw',
      age: 21
    }
  },

  mutations: {
    changeName(state) {
      console.log(state.name)
    }
  },

  modules: {
    login,
    system
  }
})

// 用户手动刷新页面时系统根据当前 localStorage 重新构建 store
export function setupStore() {
  store.dispatch('login/loadLocalLogin')
}

export function useStore(): Store<IRootWithModule> {
  return useStoreVuex()
}

export default store
